package nio;

import java.nio.ByteBuffer;

/**
 * @program: comprehensive-service
 *
 * @description:
 *
 * @author: 窦绍飞
 *
 * @create: 2021-11-25 20:27
 **/
public class ReadOnlyBuffer {

	public static void main(String[] args) {
		// 创建一个buffer
		ByteBuffer byteBuffer = ByteBuffer.allocate(64);

		for (int i = 0; i < 64; i++) {
			byteBuffer.put((byte) i);
		}
		byteBuffer.flip();

		// 读取一个只读的buffer

		ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();

		System.out.println(readOnlyBuffer.getClass());

		// 读取
		while (readOnlyBuffer.hasRemaining()) {
			System.out.println(readOnlyBuffer.get());
		}
		// 会抛出异常
		// readOnlyBuffer.putInt(1);
	}
}
